<?php

class ProjectAction extends CommonAction {
    
    public function _empty() {
        $tpl = "../Include/App/Group/Bridge/Tpl/Public_404.html";
        $this->display($tpl);
    }

    public function detail() {
        $id = I('id', 1);
        $prjInfo = M('project')->table(array('xia_project' => 'arr', 'xia_team' => 'team', 'xia_user_info'=>'uif'))->field('arr.*,team.id as teamid,team.name as tname,team.logo,team.weibo,team.weixin,uif.nickname as creater')->where('arr.id=' . $id . ' and arr.tid=team.id and team.uid=uif.uid')->find();
        $jobUsers = M('job_user')->table(array('xia_job_user' => 'list', 'xia_job' => 'job', 'xia_user_info' => 'user'))->field('list.*,job.name as jname,user.nickname,user.photo')->where('list.pid=' . $id . ' and list.jobid=job.id and list.uid=user.uid and (list.state=2 or list.state=4 or list.state=5)')->select();
        $jobs = M('job')->where('pid='.$id.' and state>0')->select();
        $favCount = M('favorites')->where('pid=' . $id)->count();
        $mefav = M('favorites')->where('pid='.$id.' and uid='.$this->uid)->find();
        if ($mefav) {
            $this->assign('mefav', 1);
        }
        $jobTotal = M('job')->where('pid='.$id.' and state>0')->sum('num');
        if(!$jobTotal){$jobTotal = 0;}
        $rest = $jobTotal - count($jobUsers);
        $rest = ($rest >= 0) ? $rest : 0;
        
        //自己申请过的
        $applied = M('job_user')->where('uid='.$this->uid.' and state=0')->select();
        $moneyin = 0;//职位薪酬托管数
        $alljob = array();
        foreach ($jobs as $key1 => $value1) {
            if ($value1['moneyin']==1 || $value1['moneyin']==2) {
                $moneyin++;
            }
            foreach ($jobUsers as $key2 => $value2) {
                if($value1['id']==$value2['jobid']){
                    $alljob[] = array('reserved'=>1,'uid'=>$value2['uid'],'uname'=>$value2['nickname'],'photo'=>$value2['photo'],'jname'=>$value2['jname'],'applied'=>0);
                }
            }
            $jrest = $value1['num']-$value1['datastate'];
            if($jrest>0){
                $item = array('reserved'=>0,'salary'=>$value1['salary'],'jname'=>$value1['name'],'rest'=>($jrest),'pid'=>$value1['pid'],'did'=>$value1['did'],'id'=>$value1['id'],'applied'=>0);
                //是否自己申请过
                foreach ($applied as $al) {
                    if ($value1['id']==$al['jobid']) {
                        $item['applied'] = 1;
                    }
                }
                $alljob[] = $item;
            }
        }
        if ($moneyin>0 && $moneyin<count($jobs)) {
            $moneyin = 3;
        }
        else if ($moneyin>=count($jobs)) {
            $moneyin = 1;
        }
        $prjInfo['moneyin'] = $moneyin;
        //屏蔽创建者email
        $prjInfo['creater'] = convEmail($prjInfo['creater']);
        
        $this->assign("prjInfo", $prjInfo);
        $this->assign('jobs', $alljob);
        $this->assign("pid", $id);
        $this->assign("uid", $this->uid);
        $this->assign("favCount", $favCount);
        $this->assign('jobTotal', $jobTotal);
        $this->assign('rest', $rest);
        $this->assign('tip', C('PRJ_MONEY_TUOGUAN_TIP_BROWSER'));
        $this->assign("on", 2);
        $this->display();
    }

    public function job() {
        $id = I('id', 1);
        $prjTable = array('xia_project' => 'arr', 'xia_team' => 'team', 'xia_user_info'=>'uif');
        $prjField = 'arr.*,team.name as tname,team.logo,team.weibo,team.weixin,uif.nickname as creater';
        $prjWhere = 'arr.id=' . $id . ' and arr.tid=team.id and team.uid=uif.uid';
        $prjInfo = M('project')->table($prjTable)->field($prjField)->where($prjWhere)->find();
        $julTable = array('xia_job_user' => 'list', 'xia_job' => 'job', 'xia_user_info' => 'user');
        $julField = 'list.*,job.name as jname,user.nickname,user.photo';
        $julWhere = 'list.pid=' . $id . ' and list.jobid=job.id and list.uid=user.uid and (list.state=2 or list.state=4 or list.state=5)';
        $jobUsers = M("job_user")->table($julTable)->field($julField)->where($julWhere)->select();
        $jlField = "j.*,(select COUNT(*) from xia_jobfav jf where j.id=jf.jobid) as fc,(select COUNT(*) from xia_job_user ju where j.id=ju.jobid and ju.state=0) as aplc";
        $jobs = M('job')->table(array('xia_job'=>'j'))->field($jlField)->where('j.pid=' . $id.' and j.state>0')->select();//职位统计
        //$jmFiled = "j.id,(select COUNT(*) from xia_jobfav if where j.id=jf.jobid and jf.uid=".$this->uid.")";
        $resMfav = M('job')->table(array('xia_jobfav'=>'jf'))->field($jmFiled)->where('jf.uid=' . $this->uid)->select();//当前用户收藏的
        $jobTotal = M('job')->where('pid=' . $id.' and state>0')->sum('num');
        if (!$jobTotal) {
            $jobTotal = 0;
        }
        $rest = $jobTotal - count($jobUsers);
        $rest = ($rest >= 0) ? $rest : 0;
        
        $alljobs = array();
        //已申请
        $applieds = M('job_user')->where('uid='.$this->uid.' and state=0')->select();
        foreach ($jobs as $job) {
            $job['applied'] = 0;
            foreach ($applieds as $applied) {
                if ($job['id']==$applied['jobid']) {
                    $job['applied'] = 1;
                }
            }
            $job['faved'] = 0;
            foreach ($resMfav as $fav) {
                if ($job['id']==$fav['jobid']) {
                    $job['faved'] = 1;
                }
            }
            $alljobs[] = $job;
        }
        
        //屏蔽创建者email
        $prjInfo['creater'] = convEmail($prjInfo['creater']);
        
        $this->assign("prjInfo", $prjInfo);
        $this->assign("jobUsers", $jobUsers);
        $this->assign("pid", $id);
        $this->assign("uid", $this->uid);
        $this->assign("jobTotal", $jobTotal);
        $this->assign('countData', count($jobUsers));
        $this->assign("rest", $rest);
        $this->assign("jobs", $alljobs);
        $this->assign('tip', C('PRJ_MONEY_TUOGUAN_TIP_BROWSER'));
        $this->assign("on", 2);
        $this->display();
    }

    //收藏项目
    public function fav_ajax() {
        $pid = I('pid');
        $uid = $this->uid;
        $data = array(
            'uid' => $uid,
            'pid' => $pid,
            'ctime' => time(),
        );
        $resf = M('favorites')->where('uid='.$uid.' and pid='.$pid)->find();
        if($resf){
            echo json_encode(array('success'=>0, 'msg'=>'您已经喜欢过这个项目'));
            exit;
        }
        $resd = M('favorites')->add($data);
        if ($resd) {
            $resPrj = M('project')->where('id='.$pid.' and state>0')->find();
            if($resPrj){
                $resf = M('team')->where("id ='" . $resPrj['tid'] . "' and state>0")->setInc('favcount');
            }
            
            echo json_encode(array('success' => 1, 'msg' => ''));
            exit;
        } else {
            echo json_encode(array('success' => 0, 'msg' => '保存失败，请重新提交'));
            exit;
        }
    }

    //取消收藏项目
    public function del_fav() {
        import('ORG.Util.Verify');
        if (Verify::isEmpty(I('fid'))==false
            || Verify::isEmpty(I('tid'))==false) {
            echo json_encode(array('success'=>0, 'msg'=>'参数错误!'));
            exit;
        }
        
        $fid = I('fid');
        $tid = I('tid');
        if ($fid > 0) {
            $ret = M('favorites')->where("id=".$fid)->delete();
            if ($ret>0) {
                $resd = M('team')->where("id ='" . $tid . "' and state>0")->setDec('favcount');
                echo json_encode(array('success' => 1, 'msg' => '已取消关注该项目'));
                exit;
            } else {
                echo json_encode(array('success' => 0, 'msg' => '由于未知原因，未能取消关注该项目'));
                exit;
            }
        } else {
            echo json_encode(array('success' => 0, 'msg' => '参数错误!'));
            exit;
        }
    }
    
    //收藏职位
    public function favjob_ajax() {
        $jobid = I('job');
        $uid = $this->uid;
        $data = array(
            'uid' => $uid,
            'jobid' => $jobid
        );
        $resf = M('jobfav')->where('uid='.$uid.' and jobid='.$jobid)->find();
        if($resf){
            $this->jsonpf('您已经喜欢过这个职位');
        }
        $resd = M('jobfav')->add($data);
        if ($resd) {
            $resf = M('team')->where("id ='" . $this->userinfo['teamid'] . "' and state>0")->setInc('favcount');//团队关注累加
            $this->jsonps();
        } else {
            $this->jsonpf('由于未知原因，未能收藏这个职位');
        }
    }

    //取消收藏职位
    public function unfavjob_ajax() {
        import('ORG.Util.Verify');
        if (Verify::isEmpty(I('job'))==false) {
            $this->jsonpf('参数错误!');
        }
        
        $fjid = I('job');
        if ($fjid > 0) {
            $ret = M('jobfav')->where("jobid=".$fjid." and uid=".$this->uid)->delete();
            if ($ret>0) {
                $resd = M('team')->where("id ='" . $this->userinfo['teamid'] . "' and state>0")->setDec('favcount');
                $this->jsonps(1,'已取消收藏这个职位');
            } else {
                $this->jsonpf('由于未知原因，未能取消收藏这个职位');
            }
        } else {
            $this->jsonpf('参数错误!');
        }
    }

    //职位申请
    public function sq_ajax() {
        $pid = I('prj', 0);
        $jobid = I('job', 0);
        
        //该职位是否已托管
        $resJob = M('job')->where('id='.$jobid)->find();
        if (!$resJob) {
            $this->jsonpf('找不到该职位！');
        }
        if ($resJob['num']<=$resJob['datastate']) {
            $this->jsonpf("这个职位没有空缺席位，不能加入！");
        }
        if ($resJob['moneyin']==\App\Common\Constvars::JOB_SALARYSTATUS_UNDELTEGETE) {
            $this->jsonpf('这个职位薪酬尚未托管,不能申请入职');
        }
        if ($resJob['moneyin']==\App\Common\Constvars::JOB_SALARYSTATUS_SETTLE) {
            $this->jsonpf('这个职位已经结算,不能申请入职');
        }
        if ($this->userinfo['utpye']==2) {
            $this->jsonpf('企业用户不能申请加入别人的团队');
        }
        
        $now = time();
        $data = array(
            'uid' => $this->uid,
            'pid' => $pid,
            'did' => 0,
            'jobid' => $jobid,
            'uname' => $this->userinfo['nickname'],
            'uptime'=>$now,
        );
        $resd = M('job_user')->add($data);
        if ($resd) {
            //向管理层发送通知
            $resJob = M('job')->where('id='.$jobid)->find();
            $resPrj = M('project')->where('id='.$pid)->find();
            $resTeam = M('team')->where('id='.$resPrj['tid'])->find();
            $content = "<td>哇哦，用户<a href=\"javascript:void(0);\" data-url=\"".genTalentURL($this->uid)."\"><b>[".$this->userinfo['nickname']."]</b></a>申请加入您的团队，任职<b>[".$resJob['name']."]</b>。</td>";
            $content = logPost($content, U("/Team/members", array("teamid"=>$resTeam['id'])), $now);
            $ret = sendNoticeBatch($this->uid, $resTeam['id'], $resPrj['id'], $content, true, true, true);
            if (!$ret) {
                $this->jsonps(1,'向管理层发送通知失败!');
            }
            //发送邮件
            $resUser = M('user')->table(array('xia_user'=>'user', 'xia_user_info'=>'uif'))->where('user.id='.$resTeam['uid']." and user.id=uif.uid")->find();
            $body = "哇哦，用户[".$this->userinfo['nickname']."]申请加入您的团队，任职<b>[".$resJob['name']."]</b>,快去虚拟办公室看看吧！";
            $body = mailPostfix($body);
            mailqueue_push($resUser['email'], $resUser['nickname'], "申请加入团队", $body);
            
            $this->jsonps(1,'申请已提交，等待项目管理者审批。');
        } else {
            $this->jsonpf('数据出错!');
        }
    }

    //职位申请判断
    public function if_ajax() {
        $uid = sVerify();
        $userinfo = M('user_info')->where('uid='.$uid)->find();
        if(empty($uid)){
            $this->jsonps(1);
        }
        else {
            if($userinfo['teamid']>0){
                $this->jsonps(2, "很遗憾！您已经是其他团队合伙人,不能加入该团队。");
            }
        }
        if ($userinfo['utype']==2) {
            $this->jsonps(5, '您是企业用户，不能申请加入别人的团队');
        }
        
        $pid = I('prj', 0);
        $jobid = I('job', 0);
        //职位是否已满员
        $resJob = M('job')->where("id='".$jobid."' and state>0")->find();
        if ($resJob['num']<=$resJob['datastate']) {
            $this->jsonpf("这个职位没有空缺席位了，不能加入！");
        }
        if ($resJob['moneyin']==\App\Common\Constvars::JOB_SALARYSTATUS_SETTLE) {
            $this->jsonpf("这个职位已结算，不能加入！");
        }
        
        $juTab = M('job_user');
        $ret = $juTab->where('uid='.$uid.' and jobid='.$jobid)->find();
        if ($ret) {
            $this->jsonps(3, "您已经申请过该职位。");
        }
        $ret = $juTab->where('uid='.$uid.' and pid='.$pid)->find();
        if ($ret) {
            $this->jsonps(4, '您已经申请过该项目下的其它职位。');
        }
        
        $this->jsonps(0);
    }
    
    //撤销申请职位
    public function cancelapply_ajax() {
        $jobid = I('job', 0);
        $ret = M('job_user')->where('uid='.$this->uid.' and jobid='.$jobid)->delete();
        if ($ret) {
            //向管理层发送通知
            $resJob = M('job')->where('id='.$jobid)->find();
            $resPrj = M('project')->where('id='.$resJob['pid'])->find();
            $resTeam = M('team')->where('id='.$resPrj['tid'])->find();
            $content = "<td>用户<a href=\"javascript:void(0);\" data-url=\"".genTalentURL($this->uid)."\"><b>".$this->userinfo['nickname']."</b></a>已经撤销对你团队中<b>[".$resJob['name']."]</b>岗位的加入申请.</td>";
            $content = logPost($content, "", time());
            $ret = sendNoticeBatch($this->uid, $resTeam['id'], $resPrj['id'], $content, true, true, true);
            //发送邮件
            $resU = M('user')->table(array('xia_user'=>'user', 'xia_user_info'=>'uif'))->where('user.id='.$resTeam['uid']." and user.id=uif.uid")->find();
            mailqueue_push($resU['email'], $resU['nickname'], "撤销入队申请", mailPostfix($content));
            
            $this->jsonps(1, '已撤销申请该职位');
        }
        else {
            $this->jsonpf('撤销失败！');
        }
    }

}

?>